package shopping.dao.impl;

import shopping.bean.Clothes;
import shopping.dao.ClothesDao;

import java.util.List;

public class ClothesDaoImp extends BaseDao implements ClothesDao {
    @Override
    public int addClothes(Clothes clothes) {
        String sql = "insert into clothes(`name`,`kind`,`price`,`sales`,`stock`,`img_path`) values(?,?,?,?,?,?)";

        return update(sql,clothes.getName(),clothes.getKind(),clothes.getPrice(),clothes.getSales(),clothes.getStock(),clothes.getImgPath());
    }

    @Override
    public int deleteClothesById(Integer id) {
        String sql = "delete from clothes where id = ?";
        return update(sql,id);
    }

    @Override
    public int updateClothes(Clothes clothes) {
        String sql = "update clothes set `name`=?,`kind`=?,`price`=?,`sales`=?,`stock`=?,`img_path`=? where id = ?";
        System.out.println(clothes);
        return update(sql,clothes.getName(),clothes.getKind(),clothes.getPrice(),clothes.getSales(),clothes.getStock(),clothes.getImgPath(),clothes.getId());
    }

    @Override
    public Clothes queryClothesById(Integer id) {
        String sql = "select `id` , `name` , `kind` , `price` , `sales` , `stock` , `img_path` imgPath from clothes where id = ?";
        return queryForOne(Clothes.class,sql,id);
    }

    @Override
    public List<Clothes> queryClothess() {
        String sql = "select `id` , `name` , `kind` , `price` , `sales` , `stock` , `img_path` imgPath from clothes";
        return queryForList(Clothes.class,sql);
    }

    @Override
    public Integer queryForPageTotalCount() {
        String sql = "select count(*) from clothes";

        Number count = (Number) queryForSingleValue(sql);
        return count.intValue();
    }

    @Override
    public List<Clothes> queryForPageItems(int begin, int pageSize) {
        String sql = "select `id` , `name` , `kind` , `price` , `sales` , `stock` , `img_path` imgPath from clothes limit ?,?";
        return queryForList(Clothes.class,sql,begin,pageSize);

    }

    @Override
    public Integer queryForPageTotalCountByPrice(int min, int max) {
        String sql = "select count(*) from clothes where price between ? and ?";

        Number count = (Number) queryForSingleValue(sql,min,max);
        return count.intValue();
    }

    @Override
    public List<Clothes> queryForPageItemsByPrice(int begin, int pageSize, int min, int max) {
        String sql = "select `id` , `name` , `kind` , `price` , `sales` , `stock` , `img_path` imgPath from clothes where price between ? and ? order by price limit ?,?";
        return queryForList(Clothes.class,sql,min,max,begin,pageSize);
    }

    @Override
    public Integer queryForPageTotalCountByName(String name) {
        String sql = "select count(*) from clothes where name = ?";

        Number count = (Number) queryForSingleValue(sql,name);
        return count.intValue();
    }

    @Override
    public List<Clothes> queryForPageItemsByName(int begin, int pageSize, String name) {
        String sql = "select `id` , `name` , `kind` , `price` , `sales` , `stock` , `img_path` imgPath from clothes where name = ? order by price limit ?,?";
        return queryForList(Clothes.class,sql,name,begin,pageSize);
    }

    @Override
    public Integer queryForPageTotalCountByKind(String kind) {
        String sql = "select count(*) from clothes where kind = ?";

        Number count = (Number) queryForSingleValue(sql,kind);
        return count.intValue();
    }

    @Override
    public List<Clothes> queryForPageItemsByKind(int begin, int pageSize, String kind) {
        String sql = "select `id` , `name` , `kind` , `price` , `sales` , `stock` , `img_path` imgPath from clothes where kind = ? order by price limit ?,?";
        return queryForList(Clothes.class,sql,kind,begin,pageSize);
    }

    @Override
    public Clothes queryClothesByName(String name) {
        String sql = "select `id` , `name` , `kind` , `price` , `sales` , `stock` , `img_path` imgPath from clothes where name = ?";
        return queryForOne(Clothes.class,sql,name);
    }
}
